import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heroe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Heroe extends Personaje
{
    private SimpleTimer timer=new SimpleTimer();
    private static final int repairsTime=8;
    private int reloadDelayRepairs;
    private int reloadDelayCount;
    private static final int gunReloadTime=10;
    private Counter monedas;
    private Mensaje mensaje;
    private int score;
    private boolean ganas;

    public Heroe(int rest)
    {
        super(rest);
        monedas=new Counter("Points: " + "");
        reloadDelayRepairs=5;  
        reloadDelayCount=5;
        ganas=false;
        //    mensaje=new Mensaje("Vidas: "+resistance,30);
    }

    public int getPuntaje()
    {
        return monedas.getValue();
    }

    public void addedToWorld(World world)
    {

        getWorld().addObject(monedas,312,40);
        // getWorld().addObject(mensaje,550,40);
    }

    public void act() 
    {
        movement();
        repairs();
        detectInsideWalls();
        activateActivador();
        pasaNivel();
        reloadDelayRepairs++;
        reloadDelayCount++;
    }    

    /*
     * Moves the Hero
     */
    public void movement()
    {
        if (Greenfoot.isKeyDown("up"))
            move(2);
        if (Greenfoot.isKeyDown("down"))
            move(-2);
        if (Greenfoot.isKeyDown("left"))
            turn(-2);
        if (Greenfoot.isKeyDown("right"))
            turn(2);         
        if(/*timer.millisElapsed()>100&&*/"space".equals(Greenfoot.getKey()))
        {
            fire();
            // timer.mark();
        }   
    }

    public void incrementaMonedas()
    {
        monedas.add(1);
    }

    /*
     *  Place the bullet at Hero's current position
     */
    public void fire()
    {
        if(reloadDelayCount>=gunReloadTime)
        { 
            Bala bala= new Bala(this);
            getWorld().addObject(bala,getX(),getY());
            bala.setRotation(getRotation());
            bala.move(30);
            reloadDelayCount=0;
        } 
    }

    /*
     * The hero won't pass through the walls
     */
    public void detectWall1()
    {
        Wall1 wall=(Wall1)getOneIntersectingObject(Wall1.class);
        Grass grass=(Grass)getOneIntersectingObject(Grass.class);
        if(wall!=null)
        {  if(getX()<300 || getX()>400 && getX()<650)         
                setLocation(getX()+5,getY());
            if(getX()>300 && getX()<400 || getX()>650)
                setLocation(getX()-5,getY());
        }

    }

    public void detectWall2()
    {
        Wall2 wall=(Wall2)getOneIntersectingObject(Wall2.class);
        if(wall!=null)
        {
            if(getY()<getWorld().getHeight()/2)
                setLocation(getX(),getY()+5);
            if(getY()>getWorld().getHeight()/2)
                setLocation(getX(),getY()-5);
        }
    }

    public void repairs()
    {
        Grass grass=(Grass)getOneIntersectingObject(Grass.class);
        if(grass!=null)
        {
            if(grass.getResistance()<100)
            {
                if(Greenfoot.isKeyDown("x"))
                {
                    if(reloadDelayRepairs>=repairsTime)  
                    {
                        grass.increaseResistance();
                        monedas.add(1);
                        reloadDelayRepairs=0;
                    }
                }
            }
        }
    }

    /*  public void decreaseResistance()
    {
    if(resistance>0)
    {
    resistance--;
    mensaje=new Mensaje("Vidas: "+resistance,30);
    getWorld().addObject(mensaje,550,40);
    }
    }*/

  /* public void detectBoxWeapon()
    {
        BoxWeapon box=(BoxWeapon)getOneIntersectingObject(BoxWeapon.class);
        if(box!=null)
        {           
            if(monedas.getValue()>=100)
            {
                if(Greenfoot.isKeyDown("z"))
                    box.randomWeapon();  
            }
        }
    }*/

    public void activateActivador()
    {
        Activador act;
        World world=getWorld();
        act=(Activador)getOneIntersectingObject(Activador.class);
        if(act!=null)
        {
            if(world instanceof Fortaleza)
            {          
                if(monedas.getValue()>=100)
                {
                    if(Greenfoot.isKeyDown("z"))
                    {
                        if(act.getActivated()==false)
                        {
                            act.setActivador(); 
                        }
                    }
                }
            }  
            if(world instanceof Fortaleza2)
            {                           
                if(monedas.getValue()>=300)
                {
                    if(Greenfoot.isKeyDown("z"))
                    {
                        if(act.getActivated()==false)
                        {
                            act.setActivador(); 
                        }
                    }
                }               
            }
            if(world instanceof Fortaleza3)
            {            
                if(monedas.getValue()>=500)
                {
                    if(Greenfoot.isKeyDown("z"))
                    {
                        if(act.getActivated()==false)
                        {
                            act.setActivador(); 
                        }
                    }
                }
            }
        }
    }

    public void detectPuerta()
    {
        World world=getWorld();
        PuertaH puerta=(PuertaH)getOneIntersectingObject(PuertaH.class);
        if(puerta!=null)
        {
            if(world instanceof Fortaleza)
            {
                if(puerta.getDisponible()==true)
                {
                    Fortaleza2 fort = new Fortaleza2();
                    Greenfoot.setWorld(fort);
                }
            }
            if(world instanceof Fortaleza2)
            {
                if(puerta.getDisponible()==true)
                {
                    Fortaleza3 fort=new Fortaleza3();
                    Greenfoot.setWorld(fort);
                }
            }
            if(world instanceof Fortaleza3)
            {
                if(puerta.getDisponible()==true)
                {
                    Fortaleza3 fort=new Fortaleza3();
                    Greenfoot.setWorld(fort);
                }
            }
        }
    }

    public void pasaNivel()
    {
        World world=getWorld();   
        if(isTouching(PuertaH.class))
            setLocation(getX(),getY()-5);
        else
        if(world instanceof Fortaleza)
        {
            if(this.getY()==getWorld().getHeight()-1)
            {
                Fortaleza2 fort = new Fortaleza2();
                Greenfoot.setWorld(fort);
            }  
        }
        if(world instanceof Fortaleza2)
        {
            if(this.getY()==getWorld().getHeight()-1)
            {
                Fortaleza3 fort = new Fortaleza3();
                Greenfoot.setWorld(fort);
            }    
        }
        if(world instanceof Fortaleza3)
        {

            if(this.getX()<15)
            {
                ganas=true;
            }    
        }
    }

    public boolean getGanas()
    {
        return ganas;
    }

    public void detectInsideWalls()
    {
        World world=getWorld();
        if(world instanceof Fortaleza)
        {
            detectWall1();
            detectWall2();
        }
        if(world instanceof Fortaleza2)
        {
            detectWall3();
            detectWall4();
        }      
        if(world instanceof Fortaleza3)
        {
            detectWall33();
            detectWall43();
        }     
    }

    public void detectWall4()
    {
        World world=getWorld();
        Wall4 wall=(Wall4)getOneIntersectingObject(Wall4.class);
        if(wall!=null)
        {
            if(getX()<655)
                setLocation(getX()+5,getY());
            if(getX()>655)
                setLocation(getX()-5,getY());
        }
    }

    public void detectWall3()
    {
        World world=getWorld();
        Wall3 wall=(Wall3)getOneIntersectingObject(Wall3.class);
        if(wall!=null)
        {
            if(getY()<300)
                setLocation(getX(),getY()+5);
            if(getY()>300)
                setLocation(getX(),getY()-5);
        }
    }

    public void detectWall33()
    {
        // World world=getWorld();
        Wall3 wall=(Wall3)getOneIntersectingObject(Wall3.class);
        //      if(world instanceof Fortaleza2)
        //    {
        if(wall!=null)
        {
            if(getY()>425)
                setLocation(getX(),getY()-5);
            if(getY()>358&&getY()<420)
                setLocation(getX(),getY()+5);
                   if(getY()>168&&getY()<302)
                setLocation(getX(),getY()-5);
                if(getY()<167)
                setLocation(getX(),getY()+5);
           //         if(getX()>620&&getY()>347&&getY()<401);
             //   setLocation(getX(),getY()-5);
        }
    }

    public void detectWall43()
    {
        // World world=getWorld();
        Wall4 wall=(Wall4)getOneIntersectingObject(Wall4.class);
        //      if(world instanceof Fortaleza2)
        //    {
        if(wall!=null)
        {

            if(getX()>262&&getX()<377||getX()>727)
                setLocation(getX()-10,getY());
//                 if(getX()>613&&getX()<713)
  //              setLocation(getX()-10,getY());
    //             if(getX()>615&&getX()<721)
       //        setLocation(getX()-10,getY());
         
        }

        /*
        public void detectBoxBonus()
        {
        BoxBonus box=(BoxBonus)getOneIntersectingObject(BoxBonus.class);
        if(box!=null)
        {           
        if(Greenfoot.isKeyDown("z"))
        System.out.println("bonus");   
        }

        }*/
    }

    public void actualizaPuntos()
    {
        if(UserInfo.isStorageAvailable())
        {
            UserInfo info=UserInfo.getMyInfo();
            if(this.getPuntaje()>info.getScore())
            {
                info.setScore(this.getPuntaje());
            }
            info.store();
        }
    }
}